package com.bjtu.laorenyouhao.event.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.Map;

import com.bjtu.common.valid.group.AddGroupp;
import com.bjtu.common.valid.group.UpdateGroup;
import com.bjtu.laorenyouhao.event.entity.EventPeopleEntity;
import com.bjtu.laorenyouhao.event.feign.GuanliyuanFeignService;
import com.bjtu.laorenyouhao.event.feign.LaorenFeignService;
import com.bjtu.laorenyouhao.event.feign.YigongFeignService;
import com.bjtu.laorenyouhao.event.feign.YuangongFeignService;
import com.bjtu.laorenyouhao.event.service.EventPeopleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.bjtu.laorenyouhao.event.entity.EventInfoEntity;
import com.bjtu.laorenyouhao.event.service.EventInfoService;
import com.bjtu.common.utils.PageUtils;
import com.bjtu.common.utils.R;



/**
 * 事件表
 *
 * @author yangyalong
 * @email yangyalong@gmail.com
 * @date 2021-07-08 21:00:01
 */
@RestController
@RequestMapping("event/eventinfo")
public class EventInfoController {
    @Autowired
    private EventInfoService eventInfoService;

    @Autowired
    private EventPeopleService eventPeopleService;

    @Autowired
    private GuanliyuanFeignService guanliyuanFeignService;

    @Autowired
    private LaorenFeignService laorenFeignService;

    @Autowired
    private YigongFeignService yigongFeignService;

    @Autowired
    private YuangongFeignService yuangongFeignService;

    @RequestMapping("/test")
    public R Test(){
        return guanliyuanFeignService.getRoomId("房间202");
    }

    @RequestMapping("/event")
    private R myEvent(@RequestBody Map data){
       // {eventName=5, room=房间202, path=2021-07-13/1626164899.4530218_snapshot_20210713_162818.jpg, renyuanType=0, renyuan=王汉功}
        //封装eventinfo
        System.out.println(data);
        EventInfoEntity eventInfoEntity = new EventInfoEntity();

        eventInfoEntity.setRoomNumber(Long.valueOf((Integer) guanliyuanFeignService.getRoomId((String) data.get("room")).get("roomId")));
        eventInfoEntity.setDescription("https://laorenyouhao.oss-cn-beijing.aliyuncs.com/" + (String) data.get("path"));
        eventInfoEntity.setEventDate(new Date());
        System.out.println((String) data.get(data.get("eventName")));
        eventInfoEntity.setEventType(Integer.valueOf((String) data.get("eventName")));
        eventInfoService.save(eventInfoEntity);

        //把人员提取出来
        String renyuanType = (String) data.get("renyuanType");
        String[] renyuanTypes = renyuanType.split("_");

        String renyuan = (String) data.get("renyuan");
        String[] renyuans = renyuan.split("_");
        //封装参与事件人员
        EventPeopleEntity eventPeopleEntity = new EventPeopleEntity();
        eventPeopleEntity.setEventId(eventInfoEntity.getId());

        //得到人员id,并且录入数据库
        for (int i = 0;i < renyuanTypes.length; i++){
            if (renyuans[i].equals("null")){
                continue;
            }

            Integer renyuanId = getRenyuanId(Integer.valueOf(renyuanTypes[i]), renyuans[i]);
            System.out.println(renyuanId);
            eventPeopleEntity.setRenyuanType(Integer.valueOf(renyuanTypes[i]));
            if (renyuanId != null){
                eventPeopleEntity.setRenyuanId(Long.valueOf(renyuanId));
            } else {
                eventPeopleEntity.setRenyuanId(null);
            }
            eventPeopleService.save(eventPeopleEntity);
        }


        return R.ok();
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = eventInfoService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		EventInfoEntity eventInfo = eventInfoService.getById(id);

        return R.ok().put("eventInfo", eventInfo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@Validated(AddGroupp.class) @RequestBody EventInfoEntity eventInfo){
		eventInfoService.save(eventInfo);
        System.out.println(eventInfo);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@Validated(UpdateGroup.class) @RequestBody EventInfoEntity eventInfo){
		eventInfoService.updateById(eventInfo);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		eventInfoService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    public  Integer getRenyuanId(Integer renyuanType,String name){
        Integer renyuanId = null;
        switch (renyuanType){
            case 0:
                renyuanId = (Integer) laorenFeignService.getLaorenId(name).get("laorenId");
                break;
            case 2:
                renyuanId = (Integer) yigongFeignService.getYigongId(name).get("yigongId");
                break;
            case 3:
                renyuanId = (Integer) yuangongFeignService.getYuangongID(name).get("yuangongId");
                break;
            default:
                break;
        }
        return renyuanId;
    }
}
